package com.rxb.blog.service;

import com.rxb.blog.mapper.ArticleCommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author：rxb
 * Date：2020-03-14 22:39
 * Description：<描述>
 */
@Service
public class ArticleCommentServiceImpl implements ArticleCommentService {
    @Autowired
    private ArticleCommentMapper articleCommentMapper;

    @Override
    public void doComment(Map map) {
        articleCommentMapper.insertComment(map);
    }

    @Override
    public List<Map> getCommentList(Map map) {
        List<Map> list = new ArrayList<>();
        List<Map> commentList = articleCommentMapper.getCommentList(map);
        commentList.forEach(m ->{
            Map<String,Object> m1 = new HashMap<>();
            m1.put("comemnt",m);

            Map<String,Object> map1 = new HashMap<>();
            map1.put("id", m.get("id"));
            List<Map> commentToCommentList = articleCommentMapper.getCommentToCommentList(map1);

            if(null != commentToCommentList && commentToCommentList.size() >0){
                commentToCommentList.forEach(m2 -> {
                    Map map2 = new HashMap<String,Object>();
                    map2.put("comment_id",m2.get("id"));
                    map2.put("reply_id",m2.get("comment_id"));

                    List<Map> commentToCommentToCommentList = articleCommentMapper.getCommentToCommentToCommentList(map2);
                    m2.put("subCommentList",commentToCommentToCommentList);
                });
            }
            m1.put("commentToComment",commentToCommentList);
            list.add(m1);
        });
        return list;
    }

    @Override
    public void doCommentToComment(Map map) {
        articleCommentMapper.doCommentToComment(map);
    }

}
